using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Contacts;
using C4F.DevKit.Contacts.Controls;

namespace C4F.DevKit.Contacts.Samples.Win
{
    public partial class MainForm : Form
	{
		#region Constructor

		public MainForm()
        {
            InitializeComponent();

			contactsListView1.ContactDoubleClick += new ContactsListView.ContactDoubleClickEventHandler(contactsListView1_ContactDoubleClick);
		}

		#endregion Constructor

		#region Methods

		#region Event Handlers

		private void mnuNewContact_Click(object sender, EventArgs e)
		{
			OnNewContactDetail();
		}
        
		private void mnuUpdateContact_Click(object sender, EventArgs e)
		{
			OnUpdateContactDetail(contactsListView1.SelectedContactID);
		}

        private void tbtnNewContact_Click(object sender, EventArgs e)
        {
            OnNewContactDetail();
        }

		private void contactsListView1_ContactDoubleClick(object sender, ContactDoubleClickArgs e)
		{
			OnUpdateContactDetail(e.ContactID);
		}

        private void tbtnRefreshContacts_Click(object sender, EventArgs e)
        {
            RefreshContacts();
        }

        private void mnuImportFromDatabase_Click(object sender, EventArgs e)
        {
            try
            {
                OnImportFromDatabase();
            }
            catch (Exception ex)
            {
                DisplayException(ex);
            }
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void mnuImportFromVCard_Click(object sender, EventArgs e)
        {
            try
            {
                OnImportFromVCard();
            }
            catch (Exception ex)
            {
                DisplayException(ex);
            }
        }
        
		#endregion Event Handlers

		#region Private

        private void DisplayException(Exception ex)
        {
            if (ex != null)
                MessageBox.Show(ex.ToString());
        }

		private void OnUpdateContactDetail(string contactID)
		{
			ContactDetailDisplay disp = new ContactDetailDisplay(contactID);
			disp.ShowDialog();
            RefreshContacts();
		}

		private void OnNewContactDetail()
		{
			ContactDetailDisplay disp = new ContactDetailDisplay();
			disp.ShowDialog();
            RefreshContacts();
		}

        private void RefreshContacts()
        {
            contactsListView1.RefreshList();
        }
        
        private void OnImportFromDatabase()
        {
            ContactDBList frm = new ContactDBList();

            if (frm.ShowDialog() == DialogResult.OK && frm.ContactID != Guid.Empty)
            {
                contactsService1.ImportContactFromDatabase(frm.ContactID);
                RefreshContacts();
            }
        }

        private void OnImportFromVCard()
        {
            contactsService1.ImportContactFromVCard();
            contactsListView1.RefreshList();
        }

		#endregion Private
               
		#endregion Methods
	}
}